package com.fxwater.admin.controller.biz;

import com.fxwater.biz.domain.PmdInfo;
import com.fxwater.biz.service.PmdService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/biz/pmd")
public class PmdController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PumpStationController.class);

    private String prefix = "biz/pmd";

    @Autowired
    private PmdService pmdService;

    /**
     * 跑马灯数据信息列表
     * @param pmdInfo
     * @return
     */
    @RequiresPermissions("biz:pmd:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (PmdInfo pmdInfo) {
        startPage();
        List<PmdInfo> list = pmdService.selectPmdInfoList(pmdInfo);
        return getDataTable(list);
    }

    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("biz:pmd:view")
    @GetMapping()
    public String pmd()
    {
        logger.info("跑马灯");
        return prefix + "/pmd";
    }

    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detail/unit/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap) {
        logger.info("====进入查看详情方法,传入的跑马灯id是:" + id + "====");
        PmdInfo pmdInfo = new PmdInfo();
        pmdInfo.setId(Long.parseLong(id));
        mmap.put("pmdInfo", pmdService.selectPmdInfoList(pmdInfo).get(0));
        return prefix +"/detail";
    }

    /**
     * 跳转修改页面
     * @param mmap
     * @return
     */
    @GetMapping("edit/{pmdId}")
    public String edit ( ModelMap mmap,@PathVariable("pmdId") String pmdId) {
        PmdInfo pmdInfo = new PmdInfo();
        pmdInfo.setId(Long.parseLong(pmdId));
        pmdInfo =  pmdService.selectPmdInfoList(pmdInfo).get(0);
        mmap.addAttribute("pmdInfo", pmdInfo);
        return prefix + "/edit";
    }

    /**
     * 保存跑马灯修改信息
     * @return
     */
    /*@RequiresPermissions("biz:pmd:edit")
    @Log(title = "公共代码", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult editSave(PmdInfo pmdInfo) {
        logger.info("跑马灯" + pmdInfo);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = simpleDateFormat.format(date);
        pmdInfo.setUpdateTime(nowTime);
        return toAjax(pmdService.editSavePmdInfo(pmdInfo));
    }*/

    /**
     * 增加跑马灯
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 添加跑马灯
     * @param pmdInfo
     * @return
     */
    /*@RequiresPermissions("biz:pmd:add")
    @Log(title = "跑马灯", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PmdInfo pmdInfo) {
        System.out.println(pmdInfo.getContent());
        return toAjax(pmdService.addPmd(pmdInfo));
    }*/

    /**
     * 删除
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:pmd:remove")
    @Log(title = "跑马灯删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(String ids) {
        return toAjax(pmdService.removePmd(ids));
    }

    /**
     * 保存跑马灯修改信息
     *
     */
    @RequestMapping(value = "editSave",method = RequestMethod.POST,produces={"application/json"})
    @ResponseBody
    public AjaxResult editSave(@RequestBody String str){
        String[] strings = str.split("\\$\\$");
        PmdInfo pmdInfo = new PmdInfo();
        String speed = strings[0];
        if(!"".equals(speed) && speed != null && !"null".equals(speed)){
            pmdInfo.setSpeed(speed);

        }
        String remark = strings[1];
        if(!"".equals(remark) && remark != null && !"null".equals(remark)){
            pmdInfo.setRemark(remark);
        }
        String id = strings[2];
        pmdInfo.setId(Long.parseLong(id));
        String content = strings[3].replace("\"","'");
        pmdInfo.setContent(content);
        return toAjax(pmdService.editSavePmdInfo(pmdInfo));
    }

    /**
     * 增加跑马灯
     *
     */
    @RequestMapping(value = "addSave",method = RequestMethod.POST,produces={"application/json"})
    @ResponseBody
    public AjaxResult addSave(@RequestBody String str){
        String[] strings = str.split("\\$\\$");
        PmdInfo pmdInfo = new PmdInfo();
        String speed = strings[0];
        if(!"".equals(speed) && speed != null && !"null".equals(speed)){
            pmdInfo.setSpeed(speed);

        }
        String remark = strings[1];
        if(!"".equals(remark) && remark != null && !"null".equals(remark)){
            pmdInfo.setRemark(remark);
        }
        String content = strings[2].replace("\"","'");
        pmdInfo.setContent(content);
        return toAjax(pmdService.addPmd(pmdInfo));
    }

}
